package com.sc.web.gwt.client.view.agentwidget.contents;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.sc.web.gwt.client.service.AgentAdminDataManagementService;
import com.sc.web.gwt.client.service.AgentAdminDataManagementServiceAsync;
import com.sc.web.gwt.client.service.UserAccountDataManagementService;
import com.sc.web.gwt.client.service.UserAccountDataManagementServiceAsync;
import com.sc.web.gwt.client.view.uicomponent.Dialog;
import com.sc.web.gwt.client.view.uicomponent.ValidatableTextBox;
import com.sc.web.gwt.shared.model.AgentAdmin;
import com.sc.web.gwt.shared.model.UserAccount;

public class AddOrEditUserAccountWidget extends Dialog {
    private static Binder binder = GWT.create(Binder.class);
    final Button confirmBtn = new Button("OK");
    final Button cancelBtn = new Button("Cancel");

    UserAccountDataManagementServiceAsync userAccountDataManagementServiceAsync = GWT.create(UserAccountDataManagementService.class);
    AgentAdminDataManagementServiceAsync agentAdminDataManagementServiceAsync = GWT.create(AgentAdminDataManagementService.class);

    private UserAccount userAccount;
//    private AdvancedTable advancedTable = null;

    @UiField
    ValidatableTextBox userName;
    @UiField
    ValidatableTextBox address;
    @UiField
    ValidatableTextBox city;
    @UiField
    ValidatableTextBox dob;
    @UiField
    ValidatableTextBox identityNumber;
    @UiField
    ValidatableTextBox nation;
    @UiField
    ListBox state;
    @UiField
    ValidatableTextBox email;
    @UiField
    ValidatableTextBox accountCode;
    @UiField
    Label popupTitle;
    @UiField
    ValidatableTextBox siteName;
    @UiField
    ValidatableTextBox firstName;
    @UiField
    ValidatableTextBox lastName;

    public interface Binder extends UiBinder<HTMLPanel, AddOrEditUserAccountWidget> {

    }

    public AddOrEditUserAccountWidget(UserAccount user, ViewAllUsersWidget parentWidget) {
        final ViewAllUsersWidget thisParentWidget = parentWidget;
        final AddOrEditUserAccountWidget thisWidget = this;
        setBody(binder.createAndBindUi(this));
        confirmBtn.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                userAccount.setAddress(address.getText());
                userAccount.setCity(city.getText());
                userAccount.setDob(dob.getText());
                userAccount.setEmail(email.getText());
                userAccount.setIdentityNumber(identityNumber.getText());
                userAccount.setNation(nation.getText());
                userAccount.setUserAccount(userName.getText());
                userAccount.setSiteName(siteName.getText());
                userAccount.setFirstName(firstName.getText());
                userAccount.setLastName(lastName.getText());
                userAccount.setState(state.getValue(state.getSelectedIndex()));
                userAccountDataManagementServiceAsync.saveOrUpdateUserAccountData(userAccount, new AsyncCallback<Boolean>() {
                    public void onFailure(Throwable throwable) {
                        throwable.printStackTrace();
                    }

                    public void onSuccess(Boolean result) {
                        if (result) {
                            thisParentWidget.refreshTable();
                            thisWidget.hide();
                        } else Window.alert("Cannot save user. Please re-check the information you have provided.");
                    }
                });
            }
        });

        cancelBtn.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                thisWidget.hide();
            }
        });

        addButton(confirmBtn);
        addButton(cancelBtn);

        userAccount = user;
        if (userAccount.getUserAccountId() == -1) {
            popupTitle.setText("Add new user");
            userName.setEnabled(true);
        } else {
            popupTitle.setText("Edit user");
            userName.setEnabled(false);
        }
        if (userAccount.getUserAccount() != null) userName.setText(userAccount.getUserAccount());
        if (userAccount.getAddress() != null) address.setText(userAccount.getAddress());
        if (userAccount.getCity() != null) city.setText(userAccount.getCity());
        if (userAccount.getDob() != null)
            dob.setText(userAccount.getDob());
        if (userAccount.getIdentityNumber() != null) identityNumber.setText(userAccount.getIdentityNumber());
        if (userAccount.getNation() != null) nation.setText(userAccount.getNation());
        if (userAccount.getState() != null) {
            if (userAccount.getStateId() == 3) {
                state.addItem("Valid");
                state.addItem("Suspended");
                state.addItem("Inactivated");
                state.setEnabled(false);
            } else {
                state.addItem("Valid");
                state.addItem("Suspended");
            }
            state.setSelectedIndex(userAccount.getStateId() - 1);
        } else {
            state.addItem("Valid");
            state.addItem("Suspended");
            state.addItem("Inactivated");
            state.setSelectedIndex(2);
            state.setEnabled(false);
        }
        if (userAccount.getEmail() != null) email.setText(userAccount.getEmail());
        if (userAccount.getAccountCode() != null) accountCode.setText(userAccount.getAccountCode());
        if (userAccount.getSiteName() != null) {
            siteName.setText(userAccount.getSiteName());
        } else {
            agentAdminDataManagementServiceAsync.getAgentAdminInfo(new AsyncCallback<AgentAdmin>() {
                public void onFailure(Throwable throwable) {
                    throwable.printStackTrace();
                }

                public void onSuccess(AgentAdmin agentAdmin) {
                    if (agentAdmin != null && agentAdmin.getSiteName() != null) {
                        siteName.setText(agentAdmin.getSiteName());
                    } else {
                        Window.alert("An error occurred while performing request. Please try again.");
                    }
                }
            });
        }
        if (userAccount.getFirstName() != null) firstName.setText(userAccount.getFirstName());
        if (userAccount.getLastName() != null) lastName.setText(userAccount.getLastName());
    }

    public void showWidget() {
        center();
        show();
    }
}